package com.liu.user.service;

import com.liu.common.utils.IdWorker;
import com.liu.user.dao.AdminDao;
import com.liu.user.entity.Admin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2019/8/21 0021.
 */
@Service
@Slf4j
public class AdminService {

    @Autowired
    private BCryptPasswordEncoder encoder;

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private IdWorker idWorker;


    public void addAdmin(Admin admin){

        admin.setId(idWorker.nextId()+"");
        admin.setPassword(encoder.encode(admin.getPassword()));
        adminDao.save(admin);
    }


    public Admin login(String loginName,String password) {
       Admin  result =adminDao.findByLoginName(loginName);
        if(result!=null && encoder.matches(password,result.getPassword())){
          return  result;
        }

       return null;
    }
}
